home *** CD-ROM | disk | FTP | other *** search
/ internet.au CDrom 42 / NETCD42.iso / web / w95 / dream2.exe / data1.cab / Program_Files / Configuration / Behaviors / Actions / Go To URL.js < prev    next >
Encoding:
JavaScript  |  1998-11-30  |  8.0 KB  |  224 lines

  1.  
  2. // Copyright 1998 Macromedia, Inc. All rights reserved.
  3. //*************** GLOBAL VARS  *****************
  4.  
  5. //******************* BEHAVIOR FUNCTION **********************
  6.  
  7. //Causes the browser to go to a new URL, or can send multiple
  8. //frames to new URLs.
  9. //The function accepts a variable number of args, in pairs as follows:
  10. //  objStr  - window or frame object reference (ex: window, parent.myFrame)
  11. //  theURL  - URL, often a filename, URL encoded. (ex: file.htm, http://www.x.com/y.htm)
  12. //
  13. //With each pair of args, sets objStr.location = theURL. Normally, you would
  14. //use a link, but this function can be used to send multiple frames to new locations.
  15. //Returns "false" to prevent a link's HREF from overriding the change.
  16.  
  17. function MM_goToURL() { //v2.0
  18.   for (var i=0; i< (MM_goToURL.arguments.length - 1); i+=2) //with arg pairs
  19.     eval(MM_goToURL.arguments[i]+".location='"+MM_goToURL.arguments[i+1]+"'");
  20.   document.MM_returnValue = false;
  21. }
  22.  
  23.  
  24. //******************* API **********************
  25.  
  26.  
  27. //Can be used with any tag and any event
  28.  
  29. function canAcceptBehavior(){
  30.   return true;
  31. }
  32.  
  33.  
  34.  
  35. //Returns a Javascript function to be inserted in HTML head with script tags.
  36.  
  37. function behaviorFunction(){
  38.   return "MM_goToURL";
  39. }
  40.  
  41.  
  42.  
  43. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  44. //Gets list of URLs from doc property. With each URL, it gets the parallel
  45. //frame name from select 'menu'. Each theURL & frameName are embedded as args,
  46. //and the URLs are encoded w/ escape().
  47.  
  48. function applyBehavior() {
  49.   var argList,numItems,i,theURL,frameName,bMain,bFrames;
  50.  
  51.   bMain = false; bFrames=false;
  52.   argList = "";
  53.   numItems = document.MM_myURLs.length;
  54.   for (i=0; i<numItems; i++) {    //with each URL
  55.     theURL = document.MM_myURLs[i];
  56.     if (theURL) {      //if not empty
  57.       frameName = document.MM_NS_REFS[i]; //get frame name from parallel prop
  58.  
  59.       if (frameName.indexOf(REF_UNNAMED) == 0)  //if unnamed reference
  60.         return MSG_FrameUnnamed;
  61.  
  62.       if (i==0) {
  63.         frameName = "parent"; //set first item correctly
  64.         bMain = true;  //there's a Main URL
  65.       } else bFrames = true;  //there's a frame URL
  66.       if (argList) argList += ",";  //if stuff already in list, add comma
  67.       argList += "'"+escQuotes(frameName)+"','"+escape(theURL)+"'";
  68.     }
  69.   }
  70.   if (argList) {
  71.     if (bMain && bFrames) //if user added URLs for MainWindow *and* frames (illegal)
  72.          return MSG_FrameMixConflict;
  73.     else return "MM_goToURL("+argList+")";  //return fn call with args
  74.   } else return MSG_MissingURL;
  75. }
  76.  
  77.  
  78.  
  79. //Returns a dummy function call to inform Dreamweaver the type of certain behavior
  80. //call arguments. This information is used by DW to fixup behavior args when the
  81. //document is moved or changed.
  82. //
  83. //It is passed an actual function call string generated by applyBehavior(), which
  84. //may have a variable list of arguments, and this should return a matching mask.
  85. //
  86. //The return values are:
  87. //  URL     : argument could be a file path, which DW will update during Save As...
  88. //  NS4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  89. //  IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  90. //  other...: argument is ignored
  91.  
  92. function identifyBehaviorArguments(fnCallStr) {
  93.   var argList, argArray, numArgGroups, i;
  94.  
  95.   argList = "";
  96.   argArray = extractArgs(fnCallStr);
  97.   numArgGroups = (argArray.length - 1) / 2; //args come in pairs
  98.   for (i=0; i<numArgGroups; i++) {          //with each frame,URL pair
  99.     argList += ((argList)?",":"")+"other,NAV";
  100.   }
  101.   return argList;
  102. }
  103.  
  104.  
  105.  
  106. //Given the original function call, this parses out the args and updates
  107. //the UI. First it calls initializeUI(). Then it gets new frame,URL pairs.
  108. //If frame already present in menu, stuff URL name in URLarray. If frame
  109. //doesn't exist, add to menu, and extend URLarray.
  110.  
  111. function inspectBehavior(upStr){
  112.   var i, j, argArray, URLarray, found, numFrames, theFrame, theURL;
  113.  
  114.   URLarray = document.MM_myURLs; //get the prior list of URLs
  115.   argArray = extractArgs(upStr); //get new list of frame,URL pairs
  116.   numFrames = document.MM_NS_REFS.length;
  117.   for (i=1; i<argArray.length; i+=2){ //with each frame,URL pair
  118.     theFrame=argArray[i];
  119.     if (theFrame.toUpperCase() == "PARENT") theFrame = TYPE_MainWindow;
  120.     theURL=unescape(argArray[i+1]);
  121.     found = false;
  122.     for (j=0; j<numFrames; j++){  //check if frame is in menu
  123.       if (document.MM_NS_REFS[j] == theFrame) { //if frame there
  124.         URLarray[j] = theURL;               //store URL at that pos
  125.         if (theURL) addStarToMenuItem(document.theForm.menu,j); //mark with  *
  126.         found = true;
  127.         break;
  128.       }
  129.     }
  130.     if (!found) alert(errMsg(MSG_FrameNotFound,theFrame,theURL)); //if frame name not found
  131.   }
  132.   document.MM_myURLs = URLarray; //save updated URL list
  133.   displayURL();         //load the URL for selected frame
  134. }
  135.  
  136.  
  137.  
  138. //***************** LOCAL FUNCTIONS  ******************
  139.  
  140.  
  141. //Load the select menu with frame names.
  142. //Also sets the global property MM_myURLs to the right num of items.
  143.  
  144. function initializeUI(){
  145.   var i,frameArray;
  146.   var URLarray = new Array;
  147.   var nameArray = new Array;
  148.  
  149.   frameArray = getObjectRefs("NS 4.0","parent","FRAME"); //get NS frame refs (IE is same)
  150.   nameArray[0] = TYPE_MainWindow;
  151.   for (i=0; i<frameArray.length; i++)   //copy array to append to MainWindow
  152.     nameArray[i+1] = frameArray[i]; //add the frame names to the list
  153.   document.MM_NS_REFS = nameArray; //store NS frame refs for later
  154.   nameArray = niceNames(nameArray,TYPE_Frame);  //convert to nice
  155.   for (i=0; i<nameArray.length; i++){
  156.     document.theForm.menu.options[i]=new Option(nameArray[i]); //add frames to menu
  157.     URLarray[i] = "";  //create blank parallel array to store URLs
  158.   }
  159.   document.MM_myURLs = URLarray; //set global
  160.  
  161.   document.theForm.theURL.focus(); //set focus on textbox
  162.   document.theForm.theURL.select(); //set insertion point into textbox
  163. }
  164.  
  165.  
  166.  
  167. //Given URL in form, looks up the menu's selection number, and stores the
  168. //new URL at that position in the global document property "MM_myURLs".
  169.  
  170. function storeURL(){
  171.   var newURL, URLarray, menuIndex, newMenuText;
  172.  
  173.   newURL = document.theForm.theURL.value;
  174.   URLarray = document.MM_myURLs; //get the prior list of URLs
  175.   menuIndex = document.theForm.menu.selectedIndex; //get index to swap
  176.   URLarray[menuIndex] = newURL;   //swap
  177.   document.MM_myURLs = URLarray;   //rewrite list
  178.   if (newURL.length > 0) {  //if non-empty, mark with  *
  179.     addStarToMenuItem(document.theForm.menu, menuIndex);
  180.   } else { //nothing to store, strip off any previous star
  181.     newMenuText = stripStar(document.theForm.menu.options[menuIndex].text); //remove if old star
  182.     document.theForm.menu.options[menuIndex]=new Option(newMenuText); //add new line to menu
  183.   }
  184.   document.theForm.menu.selectedIndex = menuIndex; //reset selection index BUG!: CAUSES BOMB!
  185. }
  186.  
  187.  
  188.  
  189. //Looks at the menu of names, and returns the URL associated with the
  190. //selected item. Example: if the 2nd menu item's selected, returns 2nd item
  191. //stored in property "MM_myURLs".
  192.  
  193. function displayURL(){
  194.   var URLarray = document.MM_myURLs; //get the list of URLs
  195.   curFrameNum = document.theForm.menu.selectedIndex; //get index selected
  196.   theURL = URLarray[curFrameNum];   //lookup URL
  197.   document.theForm.theURL.value= theURL;    //write into text field
  198.   document.theForm.menu.selectedIndex = curFrameNum; //WORKAROUND! resets the menu selection
  199. }
  200.  
  201.  
  202.  
  203. //Invokes dialog to allow user to select filename. Puts value in text input.
  204.  
  205. function browseFileAndStore(){
  206.   var fileName = "";
  207.   fileName = browseForFileURL();  //returns a local filename
  208.   if (fileName) {
  209.     document.theForm.theURL.value = fileName;
  210.     storeURL();
  211.   }
  212. }
  213.  
  214.  
  215.  
  216. //**************** GENERIC FUNCTIONS ****************
  217.  
  218. //function extractArgs(upStr){
  219. //function stripStar(theStr) {
  220. //function addStarToMenuItem(theSelect,menuIndex) {
  221. //function niceNames(objRefArray,objTypeStr) {
  222. //function nameReduce (objName) {
  223. //function errMsg() {
  224.